package com.dhz.wlzs.app.mvc;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.dhz.wlzs.app.interceptor.ContextInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {

	@Autowired
	private ContextInterceptor contextInterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(contextInterceptor).addPathPatterns("/**");
		super.addInterceptors(registry);
	}
	/**
	 * 1、extends WebMvcConfigurerAdapter覆写configureMessageConverters方法
	 * 2、注入FastJsonHttpMessageConverter
	 */
	 @Bean
	public HttpMessageConverters httpMessageConverters() {
		FastJsonHttpMessageConverter converter = fastJsonHttpMessageConverter();
		return new HttpMessageConverters(converter, protobufHttpMessageConverter());
	}

//	@Override
//	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//		converters.add(fastJsonHttpMessageConverter());
//		converters.add(mappingJackson2HttpMessageConverter());
//		converters.add(protobufHttpMessageConverter());
//	}
	//TODO 
	@Bean
	public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
		return new ProtobufHttpMessageConverter();
	}
	

	private FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
		FastJsonConfig conf = converter.getFastJsonConfig();
		conf.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
		converter.setFastJsonConfig(conf);
		// 处理中文乱码问题
		List<MediaType> fastMediaTypes = new ArrayList<>();
		fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		converter.setSupportedMediaTypes(fastMediaTypes);
		return converter;
	}

	/**
	 * 设置jackson可读格式化 actuator输出硬编码采用jackson
	 * 
	 * @author: heyiwu
	 * @return
	 */
//	@Bean
	public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
		MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
		mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper());
		return mappingJackson2HttpMessageConverter;
	}

	/**
	 * 设置jackson可读格式化
	 * 
	 * @author: heyiwu
	 * @return
	 */
//	@Bean
	public ObjectMapper objectMapper() {
		ObjectMapper objMapper = new ObjectMapper();
		objMapper.enable(SerializationFeature.INDENT_OUTPUT);
		return objMapper;
	}
//
//	@Override
//	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//		configurer.ignoreAcceptHeader(false).defaultContentType(MediaType.APPLICATION_JSON);
//	}
//
//	@Bean
//	public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
//		ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
//		resolver.setContentNegotiationManager(manager);
//		List<View> views = new ArrayList<>();
//		views.add(new com.alibaba.fastjson.support.spring.FastJsonJsonView());
//		// resolver.setDefaultViews(defaultViews);
//		List<ViewResolver> resolvers = new ArrayList<ViewResolver>();
//		// TODO
//		resolvers.add(fastJsonViewResolver());
//
//		resolver.setViewResolvers(resolvers);
//		return resolver;
//	}
//
//	@Bean
//	public ViewResolver fastJsonViewResolver() {
//		return new FastJsonViewResolver();
//	}
//
//	class FastJsonViewResolver implements ViewResolver {
//		@Override
//		public View resolveViewName(String viewName, Locale locale) throws Exception {
//			FastJsonJsonView view = new FastJsonJsonView();
//			return view;
//		}
//	}

}
